<?php 
namespace Home\Controller;

use Think\Controller;
use Think\Verify;

class LoginController extends Controller{
    public function index(){
        
        if (IS_POST){
            
            $verify = new Verify();
            
            $code = I('post.code');
            $data = I('post.');
            $rules = array(
                array('username','require','用户名不能为空'),
                array('pwd','require','密码不能为空'),
                array('code','require','验证码不能为空'),
            );
            
            $user_m = M('user');
            
            $res = $user_m->validate($rules)->create($data);
            if (!$res){
                $this->ajaxReturn(array('status'=>0,'msg'=>$user_m->getError()));
            }
            
            // 判断用户是否存在
            $user = $user_m->where(array('phone'=>$data['username'],'email'=>$data['username'],'_logic'=>'OR'))->find();
            if (!$user){
                $this->ajaxReturn(array('status'=>0,'msg'=>'用户名不存在'));
            }
            
            // 判断验证码
            $res = $verify->check($code,'login');
            if (!$res){
                $this->ajaxReturn(array('status'=>0,'msg'=>'验证码错误'));
            }
            
            // 判断密码是否正确
            if (md5($data['pwd']) != $user['password']){
                $this->ajaxReturn(array('status'=>0,'msg'=>'密码错误'));
            }
            
            // 记住登录状态
            session('user',$user);  
            
            $this->ajaxReturn(array('status'=>1));
        }else {
            
            if(!empty(I('get.code'))){ // 微信登录
                // 1.获取access_token
                $url = '';
                
            }
            
            $this->display();
        }
        
    }
    
    // 退出登录
    public function logout(){
        // 清除session
        session('user',null);
        
        $url = U('Index/index');
        
        echo '<script>location.href="'.$url.'"</script>';
    }
    
    // 获取登录验证码
    public function getCode(){
        $verify = new Verify();
        $verify->length = 4;
        // 生成验证码图片
        $verify->entry('login');
    }
    
    
    public function register(){
        
        if (IS_POST){
            $type = I('post.type');
            $data = I('post.');
            if($type == 1){ // 手机注册
                // 获取cookie值
                $phone_code= cookie('phone_code');
                
                if (empty($phone_code)){
                    $this->ajaxReturn(array('status'=>0,'msg'=>'未获取验证码或验证码已经过期'));
                }
                $email_code = explode('|', $email_code);
                
                // 获取邮箱跟验证码
                $code = $phone_code[1];
                $phone = $phone_code[0];
                
                // 验证邮箱跟验证码
                if ($phone!= $data['phone']){
                    $this->ajaxReturn(array('status'=>0,'msg'=>'手机号码不对'));
                }
                
                if ($code != $data['phone_code']){
                    $this->ajaxReturn(array('status'=>0,'msg'=>'验证码错误'));
                }
                
                // 获取数据模型
                $user_M = M('user');
                
                // 设置数据
                $user = array(
                    
                    'phone' => $data['phone'],
                    'password' => md5($user['password']),
                );
                
                // 添加数据
                $res = $user_M->add($user);
                if ($res !== false){
                    $user['id'] = $res;
                    $this->ajaxReturn(array('status'=>1,'msg'=>'注册成功'));
                }else {
                    $this->ajaxReturn(array('status'=>0,'msg'=>'数据写入失败，请重试!'));
                }
            }else { // 邮箱注册
                $res = M('user')->where(array('email'=>$data['email']))->find();
                
                if (!empty($res)){
                    $this->ajaxReturn(array('status'=>2,'msg'=>'邮箱已经存在，请直接登录'));
                }
                // 获取cookie值
                $email_code = cookie('email_code');
                
                if (empty($email_code)){
                    $this->ajaxReturn(array('status'=>0,'msg'=>'未获取验证码或验证码已经过期'));
                }
                $email_code = explode('|', $email_code);
                
                // 获取邮箱跟验证码
                $code = $email_code[1];
                $email = $email_code[0];
                
                // 验证邮箱跟验证码
                if ($email != $data['email']){
                    $this->ajaxReturn(array('status'=>0,'msg'=>'邮箱不对'));
                }
                
                if ($code != $data['email_code']){
                    $this->ajaxReturn(array('status'=>0,'msg'=>'验证码错误'));
                }
                
                // 获取数据模型
                $user_M = M('user');

                // 设置数据
                $user = array(
                    
                    'email' => $data['email'],
                    'password' => $data['email_pwd'],
                    'username' => '游客',
                    'thumb' => '/Public/Home/thumb/'.mt_rand(1,10).'.jpg',
                );
                
                // 保存用户数据
                $rules = array(
                    array('email','require','邮箱不能为空'),
                    array('email','email','邮箱格式不对'),
                );
                
                $res = $user_M->validate($rules)->create($user);
                
                if(!$res){
                    $this->ajaxReturn(array('status'=>0,'msg'=>$user_M->getError()));
                }
                $user['password'] = md5($user['password']);
                // 添加数据
                $res = $user_M->add($user);
                if ($res !== false){
                    
                    // 记录登录状态
                    
                    session('user',$user);
                    
                    $this->ajaxReturn(array('status'=>1,'msg'=>'注册成功'));
                }else {
                    $this->ajaxReturn(array('status'=>0,'msg'=>'数据写入失败，请重试!'));
                }
            }
            
        }else {
            
            $this->display();
        }
        
        
    }
    
    // 发短信
    public function sendMessage(){
        
    }
    
    // 发邮件
    public function sendEmail(){
        
        $email = I('post.email');
        
        // 获取随机验证码
        $code = mt_rand(1000,9999);
        
        // 设置邮件内容
        $body = '您此次注册个人博客的验证码是<span style="color:green;margin:0 10px;">'.$code.'</span>，请在10分钟内填写注册，若非本人处理，请忽略该邮件。';
        
        $res = send_mail($email, '个人博客注册验证码',$body);
        
        if($res){
            $email_code = $email.'|'.$code;
            // 保存验证码
            cookie('email_code',$email_code,60*10);
            $this->ajaxReturn(array('status'=>1));
        }else{
            $this->ajaxReturn(array('status'=>0));
        }
    }
}

?>